﻿using System;
using FU.Capstones.IMS.Web.Common.InventoryServiceProxy.Interfaces;
using System.ServiceModel;
using System.Threading;
using System.ServiceModel.Channels;
using System.Net;
using System.ComponentModel;

namespace FU.Capstones.IMS.Web.Common.InventoryServiceProxy.OperationContracts
{
    public partial class InventoryServiceClient : ClientBase<IInventoryService>, IInventoryService
    {
        private BeginOperationDelegate onBeginOpenDelegate;

        private EndOperationDelegate onEndOpenDelegate;

        private SendOrPostCallback onOpenCompletedDelegate;

        private BeginOperationDelegate onBeginCloseDelegate;

        private EndOperationDelegate onEndCloseDelegate;

        private SendOrPostCallback onCloseCompletedDelegate;

        public InventoryServiceClient()
        {
        }

        public InventoryServiceClient(string endpointConfigurationName) :
            base(endpointConfigurationName)
        {
        }

        public InventoryServiceClient(string endpointConfigurationName, string remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
        {
        }

        public InventoryServiceClient(string endpointConfigurationName,EndpointAddress remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
        {
        }

        public InventoryServiceClient(Binding binding,EndpointAddress remoteAddress) :
            base(binding, remoteAddress)
        {
        }

        public CookieContainer CookieContainer
        {
            get
            {
                var httpCookieContainerManager = InnerChannel.GetProperty<IHttpCookieContainerManager>();
                if ((httpCookieContainerManager != null))
                {
                    return httpCookieContainerManager.CookieContainer;
                }
                return null;
            }
            set
            {
                var httpCookieContainerManager = InnerChannel.GetProperty<IHttpCookieContainerManager>();
                if ((httpCookieContainerManager != null))
                {
                    httpCookieContainerManager.CookieContainer = value;
                }
                else
                {
                    throw new InvalidOperationException("Unable to set the CookieContainer. Please make sure the binding contains an HttpC" +
                            "ookieContainerBindingElement.");
                }
            }
        }

        public event EventHandler<AsyncCompletedEventArgs> OpenCompleted;

        public event EventHandler<AsyncCompletedEventArgs> CloseCompleted;

        private IAsyncResult OnBeginOpen(object[] inValues, AsyncCallback callback, object asyncState)
        {
            return ((ICommunicationObject)(this)).BeginOpen(callback, asyncState);
        }

        private object[] OnEndOpen(IAsyncResult result)
        {
            ((ICommunicationObject)(this)).EndOpen(result);
            return null;
        }

        private void OnOpenCompleted(object state)
        {
            if ((OpenCompleted != null))
            {
                var e = ((InvokeAsyncCompletedEventArgs)(state));
                OpenCompleted(this, new AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
            }
        }

        public void OpenAsync()
        {
            OpenAsync(null);
        }

        public void OpenAsync(object userState)
        {
            if ((onBeginOpenDelegate == null))
            {
                onBeginOpenDelegate = new BeginOperationDelegate(OnBeginOpen);
            }
            if ((onEndOpenDelegate == null))
            {
                onEndOpenDelegate = new EndOperationDelegate(OnEndOpen);
            }
            if ((onOpenCompletedDelegate == null))
            {
                onOpenCompletedDelegate = new SendOrPostCallback(OnOpenCompleted);
            }
            InvokeAsync(onBeginOpenDelegate, null, onEndOpenDelegate, onOpenCompletedDelegate, userState);
        }

        private IAsyncResult OnBeginClose(object[] inValues, AsyncCallback callback, object asyncState)
        {
            return ((ICommunicationObject)(this)).BeginClose(callback, asyncState);
        }

        private object[] OnEndClose(IAsyncResult result)
        {
            ((ICommunicationObject)(this)).EndClose(result);
            return null;
        }

        private void OnCloseCompleted(object state)
        {
            if ((CloseCompleted != null))
            {
                var e = ((InvokeAsyncCompletedEventArgs)(state));
                CloseCompleted(this, new AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
            }
        }

        public void CloseAsync()
        {
            CloseAsync(null);
        }

        public void CloseAsync(object userState)
        {
            if ((onBeginCloseDelegate == null))
            {
                onBeginCloseDelegate = new BeginOperationDelegate(OnBeginClose);
            }
            if ((onEndCloseDelegate == null))
            {
                onEndCloseDelegate = new EndOperationDelegate(OnEndClose);
            }
            if ((onCloseCompletedDelegate == null))
            {
                onCloseCompletedDelegate = new SendOrPostCallback(OnCloseCompleted);
            }
            InvokeAsync(onBeginCloseDelegate, null, onEndCloseDelegate, onCloseCompletedDelegate, userState);
        }

        protected override IInventoryService CreateChannel()
        {
            return new InventoryServiceClientChannel(this);
        }

        private partial class InventoryServiceClientChannel : ChannelBase<IInventoryService>, IInventoryService
        {
            public InventoryServiceClientChannel(ClientBase<IInventoryService> client) : base(client)
            {
            }
        }

       
    }
}
